/*******************************************************************************
 * Copyright (c) 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui.internal.dnd;

import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.PerforceException;
import net.sourceforge.perforce.core.api.IPerforceServer;
import net.sourceforge.perforce.core.resources.IChangelist;
import net.sourceforge.perforce.core.resources.IP4File;
import net.sourceforge.perforce.ui.PerforceUIPlugin;
import net.sourceforge.perforce.ui.internal.Policy;

import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerDropAdapter;
import org.eclipse.swt.dnd.TransferData;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;

/**
  * @version $Revision: 169 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class PendingChangelistDropAdapter extends ViewerDropAdapter 
{
	// --------------------------------------------------------------------------- Private Variables

	/** Stores the current TransferData. */	
	private TransferData currentTransfer;
	/** Stores the current target object. */
	private IChangelist currentTarget;

	// ------------------------------------------------------------------------- Public Constructors
	
	/** Constructor for PendingChangelistDropAdapter.
	  *
	  * @param viewer the associated viewer. 
	  */ 
	public PendingChangelistDropAdapter(Viewer viewer) 
	{
		super(viewer);
	}
	
	// ------------------------------------------------------------------------------ Public Methods
	
	/** @see ViewerDropAdapter#performDrop(Object) */
	public boolean performDrop(Object data) 
	{
		IPerforceServer server = currentTarget.getServer();
		
		IP4File[] localFiles = (IP4File[]) data;
		try
		{
			for (int i = 0; i < localFiles.length; i++)
			{
				if (localFiles[i].getServer() == server)
				{
					localFiles[i].getServer().reopen(
						currentTarget.getId(), 
						new String[]{ localFiles[i].getLocation().toOSString() });
				}
			}
		}
		catch (final PerforceException e)
		{
			final Shell shell = PerforceUIPlugin.getActiveShell();
			shell.getDisplay().asyncExec(new Runnable() 
			{
				public void run() 
				{
					ErrorDialog.openError(
						shell,
						Policy.bind("simpleInternal"),
						"Failed to reopen one or more files in the selected changelist.",
						e.getStatus());
				}
			});
		}			
		finally
		{
			IPerforceServer[] servers = new IPerforceServer[]{ server }; 
			PerforceCorePlugin.getPlugin().broadcastChangelistStateChanges(servers);
		}
		
		return true;
	}

	/** @see ViewerDropAdapter#validateDrop(Object, int, TransferData) */
	public boolean validateDrop(Object target, int operation, TransferData transferType) 
	{
		currentTransfer = transferType;
		
		if (!(target instanceof IChangelist))
		{
			currentTarget = null;	
			return false;
		}
		else
		{
			if (LocalFileTransfer.getInstance().isSupportedType(transferType))
			{
				currentTarget = (IChangelist) target;
				return true;
			}
			else
			{
				return false;
			}	
		}
	}
}
